//
//  ZYXChooseCourseVC.m
//  RuiBeiKang
//
//  Created by iOS开发1 on 2019/1/9.
//  Copyright © 2019年 JW. All rights reserved.
//

#import "ZYXChooseCourseVC.h"
#import "ZYXChooseTabHeader.h"
#import "ZYXChooseHeaderView.h"
#import "ZYXChooseCoCell.h"
#import "ZYXVideoPlayVC.h"

#import "ZYXClassificationModel.h"
@interface ZYXChooseCourseVC ()<ZYXChooseHeaderViewDelegate>
@property (strong,nonatomic)ZYXChooseTabHeader *headerView;
@property (strong,nonatomic) NSMutableArray *dataArr;
///页数
@property (strong,nonatomic) NSString *page;
///类型
@property (strong,nonatomic) NSString *typeStr;

@property (strong,nonatomic) ZYXClassificationModel *mesModel;
@end

@implementation ZYXChooseCourseVC

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.title = @"行政管理";

}
-(void)setupViews{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-NavigationBar_Height) style:UITableViewStylePlain];
    [self.tableView registerNib:[UINib nibWithNibName:@"ZYXChooseCoCell" bundle:nil] forCellReuseIdentifier:@"ZYXChooseCoCell"];
     [self.tableView registerNib:[UINib nibWithNibName:@"ZYXChooseHeaderView" bundle:nil] forHeaderFooterViewReuseIdentifier:@"ZYXChooseHeaderView"];
    self.headerView = [[NSBundle mainBundle] loadNibNamed:@"ZYXChooseTabHeader" owner:nil options:nil].lastObject;
    
//    self.headerView.frame =  CGRectMake(0, 0, SCREEN_WIDTH, 200);
    self.tableView.delegate  = self;
    self.tableView.dataSource = self;
    self.tableView.showsVerticalScrollIndicator =  NO;
    self.tableView.tableHeaderView = self.headerView;
    [self.view addSubview:self.tableView];
    
    [self setupRefresh];
    self.typeStr = @"2";
    self.page = @"1";
    [self loadRequestPage:self.page];
}
#pragma mark 加载数据
- (void)setupRefresh {
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = @"1";
        [self.dataArr removeAllObjects];
        [self loadRequestPage:self.page];
    }];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        self.page = SF(@"%ld",[self.page integerValue] + 1);
        [self loadRequestPage:self.page];
    }];
    /// 设置contentInset 补缺
    self.tableView.mj_footer.ignoredScrollViewContentInsetBottom = HomeIndicator_Height;
}
- (void)endRefresh {
    [self.tableView.mj_footer endRefreshing];
    [self.tableView.mj_header endRefreshing];
}
- (void)loadRequestPage:(NSString *)page {
    [NetWork GETWithUrlString:kInterfaceAllUrl(PATH_XuankeIndexTwo) parmas:@{@"xid":self.mid,@"cate":self.typeStr,@"page":page} successBloock:^(NSDictionary *responseObject) {
        NSLog(@"%@",responseObject);
        if ([responseObject[@"code"] isEqualToNumber:@200]) {
            
            if ([self.page integerValue] <= [responseObject[@"data"][@"last_page"] integerValue]) {
                
                NSArray *modelArr = [ZYXClassificationModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"data"]];
                [self.dataArr addObjectsFromArray:modelArr];
       
                
            }else{
                [JKToast showWithText:@"暂无加载更多内容"];
            }
            
            self.mesModel = [ZYXClassificationModel mj_objectWithKeyValues:responseObject[@"mes"]];
            self.headerView.model = self.mesModel;
        
        }else{
            [JKToast showWithText:responseObject[@"message"]];
        }
        
        [self.tableView reloadData];
        [self endRefresh];
        
    } failureBlock:^(NSError *error) {
        [self endRefresh];
    }];
}
#pragma mark tableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZYXChooseCoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZYXChooseCoCell"];
    cell.model = self.dataArr[indexPath.row];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return HEIGHT(95);
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"点击事件");
    ZYXClassificationModel *model = self.dataArr[indexPath.row];
    ZYXVideoPlayVC *vc = [[ZYXVideoPlayVC alloc]init];
    vc.title = model.title;
    vc.zid = model.uid;
    [self.navigationController pushViewController:vc animated:YES];
    
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    ZYXChooseHeaderView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"ZYXChooseHeaderView"];
 
    view.delegate = self;
    return view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return HEIGHT(40);
}
#pragma mark ZYXChooseHeaderViewDelegate
-(void)clickTypeBtn:(NSInteger)tag{
    NSLog(@"点击了更换");
    if (tag == 1000) {
        self.typeStr = @"2";
    }else{
        self.typeStr = @"3";
    }
    
    self.page = @"1";
    [self.dataArr removeAllObjects];
    [self loadRequestPage:self.page];
}
#pragma mark 懒加载
-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}


@end
